package com.ruoyi.hospital.service.prescriptionInfo.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.domain.pharmacistInfo.PharmacistInfo;
import com.ruoyi.hospital.domain.prescriptionInfo.PrescriptionDrug;
import com.ruoyi.hospital.domain.shop.PharmacyDrugSku;
import com.ruoyi.hospital.domain.storeInfo.PharmacyDruginfo;
import com.ruoyi.hospital.mapper.pharmacistInfo.PharmacistInfoMapper;
import com.ruoyi.hospital.mapper.prescriptionInfo.PrescriptionDrugMapper;
import com.ruoyi.hospital.mapper.shop.PharmacyDrugSkuMapper;
import com.ruoyi.hospital.mapper.storeInfo.PharmacyDruginfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.prescriptionInfo.PrescriptionInfoMapper;
import com.ruoyi.hospital.domain.prescriptionInfo.PrescriptionInfo;
import com.ruoyi.hospital.service.prescriptionInfo.IPrescriptionInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 处方信息Service业务层处理
 * 
 * @author wendao
 * @date 2024-08-20
 */
@Service
public class PrescriptionInfoServiceImpl implements IPrescriptionInfoService 
{
    @Autowired
    private PrescriptionInfoMapper prescriptionInfoMapper;
    @Autowired
    private PrescriptionDrugMapper prescriptionDrugMapper;

    @Autowired
    private PharmacyDruginfoMapper pharmacyDruginfoMapper;

    @Autowired
    private PharmacyDrugSkuMapper pharmacyDrugSkuMapper;

    @Autowired
    private PharmacistInfoMapper pharmacistInfoMapper;
    /**
     * 查询处方信息
     * 
     * @param id 处方信息主键
     * @return 处方信息
     */
    @Override
    public PrescriptionInfo selectPrescriptionInfoById(Integer id)
    {
        return prescriptionInfoMapper.selectPrescriptionInfoById(id);
    }

    @Override
    public PrescriptionInfo selectPrescriptionInfoByConId(Integer conId) {
        return prescriptionInfoMapper.selectPrescriptionInfoByConId(conId);
    }

    /**
     * 查询处方信息列表
     * 
     * @param prescriptionInfo 处方信息
     * @return 处方信息
     */
    @Override
    public List<PrescriptionInfo> selectPrescriptionInfoList(PrescriptionInfo prescriptionInfo)
    {
        return prescriptionInfoMapper.selectPrescriptionInfoList(prescriptionInfo);
    }

    @Override
    public List<PrescriptionInfo> selectPrescriptionInfoListByPha(PrescriptionInfo prescriptionInfo) {
        return prescriptionInfoMapper.selectPrescriptionInfoListByPha(prescriptionInfo);
    }

    /**
     * 新增处方信息
     * 
     * @param prescriptionInfo 处方信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPrescriptionInfo(PrescriptionInfo prescriptionInfo)
    {
        prescriptionInfo.setCreateTime(DateUtils.getNowTimeSecond());

        //进行处方的分配，查询审方中最少的唯一  一个药师

        PharmacistInfo lessPre = pharmacistInfoMapper.getLessPre();
        if(lessPre!=null){
            prescriptionInfo.setPhaId(lessPre.getId());
        }

        int i = prescriptionInfoMapper.insertPrescriptionInfo(prescriptionInfo);
        List<PrescriptionDrug> prescriptionDrugs = prescriptionInfo.getPrescriptionDrugs();
        if(prescriptionDrugs.size()>0){
            for(PrescriptionDrug prescriptionDrug : prescriptionDrugs){
                prescriptionDrug.setPid(prescriptionInfo.getId());
                //查询商品sku信息，计算总价
//                PharmacyDruginfo pharmacyDruginfo = pharmacyDruginfoMapper.selectPharmacyDruginfoByDrugId(prescriptionDrug.getDrugId());

                PharmacyDrugSku pharmacyDrugSku = pharmacyDrugSkuMapper.selectPharmacyDrugSkuById(prescriptionDrug.getSkuId());
                prescriptionDrug.setDrugPriceTotal(pharmacyDrugSku.getPrice().multiply(BigDecimal.valueOf(prescriptionDrug.getDrugNum())));
                prescriptionDrugMapper.insertPrescriptionDrug(prescriptionDrug);
            }
        }



        return i;
    }

    /**
     * 修改处方信息
     * 
     * @param prescriptionInfo 处方信息
     * @return 结果
     */
    @Override
    public int updatePrescriptionInfo(PrescriptionInfo prescriptionInfo)
    {
        prescriptionInfo.setUpdateTime(DateUtils.getNowTimeSecond());
        return prescriptionInfoMapper.updatePrescriptionInfo(prescriptionInfo);
    }

    @Override
    public int auditPrescriptionInfo(PrescriptionInfo prescriptionInfo) {

        //查询药师信息
        PharmacistInfo pharmacistInfo = pharmacistInfoMapper.selectPharmacistInfoById(prescriptionInfo.getPhaId());

        if(pharmacistInfo!=null){
            if(StringUtils.isNullOrZero(pharmacistInfo.getReviewCount())){
                pharmacistInfo.setReviewCount(1);
            }else{
                pharmacistInfo.setReviewCount(pharmacistInfo.getReviewCount()+1);
            }
            pharmacistInfoMapper.updatePharmacistInfo(pharmacistInfo);
        }
        prescriptionInfo.setUpdateTime(DateUtils.getNowTimeSecond());
        prescriptionInfo.setPrescriptionAuditTime(DateUtils.getNowTimeSecond());
        return prescriptionInfoMapper.updatePrescriptionInfo(prescriptionInfo);
    }

    /**
     * 批量删除处方信息
     * 
     * @param ids 需要删除的处方信息主键
     * @return 结果
     */
    @Override
    public int deletePrescriptionInfoByIds(Long[] ids)
    {
        return prescriptionInfoMapper.deletePrescriptionInfoByIds(ids);
    }

    /**
     * 删除处方信息信息
     * 
     * @param id 处方信息主键
     * @return 结果
     */
    @Override
    public int deletePrescriptionInfoById(Long id)
    {
        return prescriptionInfoMapper.deletePrescriptionInfoById(id);
    }

    @Override
    public Integer waitNum(int prescriptionState, int phaId) {
        return prescriptionInfoMapper.waitNum(prescriptionState,phaId);
    }

    @Override
    public Integer number(int type, int phaId) {
        return prescriptionInfoMapper.number(type, phaId);
    }
}
